package github.kawaiior.yuanchuxiuzhen.util;

import java.text.DecimalFormat;
import java.util.Random;

public class MathUtil {
    public static String formatFloat(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();

        StringBuilder pattern = new StringBuilder("#.");
        for (int i = 0; i < places; i++) {
            pattern.append("#");
        }

        DecimalFormat df = new DecimalFormat(pattern.toString());
        return df.format(value);
    }

    public static final Random RANDOM = new Random();

    /**
     * 根据给定的概率返回 true 或 false。
     * @param probability 一个介于 0 和 1 之间的浮点数，表示返回 true 的概率。
     * @return 如果随机选择的数小于或等于给定的概率，则为 true，否则为 false。
     */
    public static boolean checkProbability(float probability) {
        if (probability < 0 || probability > 1) {
            throw new IllegalArgumentException("Probability must be between 0 and 1.");
        }
        return RANDOM.nextFloat() < probability;
    }

}
